package com.glp.work.flow.client;

import java.util.List;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.glp.common.enums.ReturnEnum;
import com.glp.common.reponse.BaseResult;
import com.glp.work.flow.request.BatchOrder;
import com.glp.work.flow.request.FlowTaskHistoryDTO;
import com.glp.work.flow.request.ToDoTasksRequest;
import com.glp.work.flow.request.WfOrderDetailQueryBySaDTO;
import com.glp.work.flow.request.WfOrderDetailQueryDTO;
import com.glp.work.flow.request.WfOrderDetailQueryDoneDTO;
import com.glp.work.flow.request.WfOrderDetailQueryToDoDTO;
import com.glp.work.flow.request.WfOrderQueryDTO;
import com.glp.work.flow.response.PageInfoResponse;
import com.glp.work.flow.response.ProcessResponse;
import com.glp.work.flow.response.ToDoTasksResponse;
import com.glp.work.flow.response.WfOrderResponse;
import com.glp.work.flow.response.WfOrderResultDTO;

import feign.hystrix.FallbackFactory;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 工作流查询
 *
 * @author pengjia
 * @version 2020年5月26日 上午8:58:28
 */
@FeignClient(value = "${work.flow.feign.server.key:GLP-WORK-FLOW}", path = "/api/query", fallbackFactory = WfProcessQueryClient.ClientFallback.class)
public interface WfProcessQueryClient {

    @ApiOperation(value = "查询待办工单")
    @PostMapping(value = "/client/queryToDoTasks", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    BaseResult<PageInfoResponse<WfOrderResultDTO>> queryToDoTasks(@RequestBody WfOrderQueryDTO wfOrderQueryDTO);

    @ApiOperation(value = "查询已办工单")
    @PostMapping(value = "/client/queryDoneTasks", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    BaseResult<PageInfoResponse<WfOrderResultDTO>> queryDoneTasks(@RequestBody WfOrderQueryDTO wfOrderQueryDTO);

    @ApiOperation(value = "查询我的工单2.0")
    @PostMapping(value = "/client/queryMyTasks", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    BaseResult<PageInfoResponse<WfOrderResultDTO>> queryMyOrder(@RequestBody WfOrderQueryDTO wfOrderQueryDTO);

    @ApiOperation(value = "查询我申请的工单2.0")
    @PostMapping(value = "/client/queryMyApplicationOrder", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    BaseResult<PageInfoResponse<WfOrderResultDTO>> queryMyApplicationOrder(@RequestBody WfOrderQueryDTO wfOrderQueryDTO);

    @ApiOperation(value = "待办任务数量统计查询")
    @PostMapping(value = "/client/totalToDoTasks", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    BaseResult<List<ToDoTasksResponse>> totalToDoTasks(@RequestBody ToDoTasksRequest req);

    @ApiOperation(value = "待办任务数量统计查询Headers")
    @RequestMapping(value = "/client/totalToDoTasks/headers", method = RequestMethod.POST)
    @ResponseBody
    BaseResult<ToDoTasksResponse> querytotalToDoTasksHeaders(@RequestHeader(required = false) String appId, @RequestHeader String userNo);


    @ApiOperation(value = "审批历史查询接口")
    @GetMapping(value = "/queryHistory/{orderId}")
    @ResponseBody
    BaseResult<List<FlowTaskHistoryDTO>> queryHistory(@PathVariable String orderId, @RequestParam(name = "appId") String appId);

    @ApiOperation(value = "查询代办工单详情")
    @PostMapping(value = "/client/queryOrderDetail/todo")
    @ResponseBody
    BaseResult<WfOrderResponse> queryToDoOrderDetail(@RequestBody WfOrderDetailQueryToDoDTO wfOrderDetailQueryDTO);

    @ApiOperation(value = "查询代办工单详情 2.0 (领取任务)")
    @PostMapping(value = "/client/queryOrderDetail/claimTask")
    @ResponseBody
    BaseResult<WfOrderResponse> queryOrderDetailClaimTask(@RequestBody WfOrderDetailQueryDTO wfOrderDetailQueryDTO);

    @ApiOperation(value = "查询已办工单详情")
    @PostMapping(value = "/client/queryOrderDetail/done")
    @ResponseBody
    BaseResult<WfOrderResponse> queryDoneOrderDetail(@RequestBody WfOrderDetailQueryDoneDTO wfOrderDetailQueryDTO);

    @ApiOperation(value = "查询工单详情")
    @PostMapping(value = "/client/queryOrderDetail")
    @ResponseBody
    BaseResult<WfOrderResponse> queryOrderDetail(@RequestBody WfOrderDetailQueryDoneDTO wfOrderDetailQueryDTO);
    
    @ApiOperation(value = "查询工单详情,不做权限校验")
    @PostMapping(value = "/client/sa/queryOrderDetail")
    @ResponseBody
    BaseResult<WfOrderResponse> queryOrderDetailBySa(@RequestBody WfOrderDetailQueryBySaDTO wfOrderDetailQueryDTO);

    @ApiOperation(value = "可驳回节点查询")
    @GetMapping(value = "/client/queryHistory")
    @ResponseBody
    BaseResult<List<FlowTaskHistoryDTO>> queryHistoryUserTask(
            @RequestParam(name = "procDefKey") String taskId,
            @RequestParam(name = "processId") String processId);

    @ApiOperation(value = "查询批量审批工单")
    @PostMapping(value = "/client/getBatchOrder")
    @ResponseBody
    public BaseResult<BatchOrder> getBatchOrder(@RequestHeader String userNo, @RequestBody List<String> processesIds);

    @ApiOperation(value = "查询所有已启用的流程")
    @GetMapping(value = "/client/getKeyList")
    @ResponseBody
    public BaseResult<List<ProcessResponse>> getKeyList(@RequestParam(value = "appId") String appId);

    @Component
    @Slf4j
    public class ClientFallback implements FallbackFactory<WfProcessQueryClient> {

        @Override
        public WfProcessQueryClient create(Throwable cause) {
            return new WfProcessQueryClient() {
                @Override
                public BaseResult<PageInfoResponse<WfOrderResultDTO>> queryToDoTasks(WfOrderQueryDTO wfOrderQueryDTO) {
                    log.error("查询待办任务失败！req:{}", JSON.toJSONString(wfOrderQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询待办任务失败！req：" + JSON.toJSONString(wfOrderQueryDTO));
                }

                @Override
                public BaseResult<PageInfoResponse<WfOrderResultDTO>> queryDoneTasks(WfOrderQueryDTO wfOrderQueryDTO) {
                    log.error("查询已办任务失败！req:{}", JSON.toJSONString(wfOrderQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询已办任务失败！req：" + JSON.toJSONString(wfOrderQueryDTO));
                }

                @Override
                public BaseResult<PageInfoResponse<WfOrderResultDTO>> queryMyOrder(WfOrderQueryDTO wfOrderQueryDTO) {
                    log.error("查询我的任务失败！req:{}", JSON.toJSONString(wfOrderQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询我的任务失败！req：" + JSON.toJSONString(wfOrderQueryDTO));
                }

                @Override
                public BaseResult<PageInfoResponse<WfOrderResultDTO>> queryMyApplicationOrder(WfOrderQueryDTO wfOrderQueryDTO) {
                    log.error("查询我申请的任务失败！req:{}", JSON.toJSONString(wfOrderQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询我申请的任务失败！req：" + JSON.toJSONString(wfOrderQueryDTO));
                }

                @Override
                public BaseResult<List<ToDoTasksResponse>> totalToDoTasks(ToDoTasksRequest req) {
                    log.error("待办任务数量统计查询！req:{}", JSON.toJSONString(req), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "待办任务数量统计查询！req：" + JSON.toJSONString(req));
                }

                @Override
                public BaseResult<ToDoTasksResponse> querytotalToDoTasksHeaders(String appId, String userNo) {
                    log.error("待办任务数量统计查询！userNo:{},appId:{}", userNo, appId);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "待办任务数量统计查询！userNo：" + userNo + "appId:" + appId);
                }

                @Override
                public BaseResult<List<FlowTaskHistoryDTO>> queryHistory(String orderId, String appId) {
                    log.error("审批历史查询失败！，appId：" + appId + "orderId:" + orderId, cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "审批历史查询失败！orderId：" + orderId + "，appId：" + appId);
                }

                @Override
                public BaseResult<WfOrderResponse> queryToDoOrderDetail(WfOrderDetailQueryToDoDTO wfOrderDetailQueryDTO) {
                    log.error("查询待办详情失败！orderId:{}，appId：" + JSON.toJSONString(wfOrderDetailQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询待办详情失败！req：" + JSON.toJSONString(wfOrderDetailQueryDTO));
                }

                @Override
                public BaseResult<WfOrderResponse> queryOrderDetailClaimTask(WfOrderDetailQueryDTO wfOrderDetailQueryDTO) {
                    log.error("查询待办详情失败！orderId:{}，appId：" + JSON.toJSONString(wfOrderDetailQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询待办详情失败！req：" + JSON.toJSONString(wfOrderDetailQueryDTO));
                }

                @Override
                public BaseResult<WfOrderResponse> queryDoneOrderDetail(WfOrderDetailQueryDoneDTO wfOrderDetailQueryDTO) {
                    log.error("查询已办详情失败！orderId:{}，appId：" + JSON.toJSONString(wfOrderDetailQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询已办详情失败！req：" + JSON.toJSONString(wfOrderDetailQueryDTO));
                }

                @Override
                public BaseResult<WfOrderResponse> queryOrderDetail(WfOrderDetailQueryDoneDTO wfOrderDetailQueryDTO) {
                    log.error("查询详情失败！orderId:{}，appId：" + JSON.toJSONString(wfOrderDetailQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询详情失败！req：" + JSON.toJSONString(wfOrderDetailQueryDTO));
                }

                @Override
                public BaseResult<List<FlowTaskHistoryDTO>> queryHistoryUserTask(String taskId, String processId) {
                    log.error("可驳回节点查询失败！processId:{}" + processId, cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "可驳回节点查询失败" + processId);
                }


                @Override
                public BaseResult<BatchOrder> getBatchOrder(String userNo, List<String> processesIds) {
                    log.error("查询批量审批工单失败！processId:{}", processesIds, cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询批量审批工单失败" + processesIds);
                }

                @Override
                public BaseResult<List<ProcessResponse>> getKeyList(String appId) {
                    log.error("查询所有已启用的流程信息失败！appId:{}", appId, cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询所有已启用的流程信息失败");
                }

				@Override
				public BaseResult<WfOrderResponse> queryOrderDetailBySa(WfOrderDetailQueryBySaDTO wfOrderDetailQueryDTO) {
                    log.error("查询详情失败queryOrderDetailBySa！orderId:{}，appId：" + JSON.toJSONString(wfOrderDetailQueryDTO), cause);
                    return new BaseResult<>(ReturnEnum.ERROR.getCode(), "查询详情失败！req：" + JSON.toJSONString(wfOrderDetailQueryDTO));
				}
            };
        }

    }

}
